//
//  GroupService.m
//  Guazi
//
//  Created by koolearn on 11-5-27.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "GroupService.h"
#import "FriendGroup.h"
#import "TwitterClient.h"
#import "JSON.h"
#import "OperationLog.h"
#import "GuaziAppDelegate.h"
#import "DBConnection.h"
#import "DataUploadService.h"


@implementation GroupService


+ (BOOL) addGroup:(FriendGroup*)group
{
    group.groupId = [DBConnection createRandomNum:kPKBitNumberGroup];
    
    //BOOL result = [TwitterClient createUserGroupSync:group];
 
    [group updateDB];

    NSDictionary* dic = [group dictionary];
    NSString* json = [dic JSONRepresentation];
    
    OperationLog* operationLog = [[OperationLog alloc] initWithModule:kOperationLogModuleGroup 
                                                            operation:kOperationLogOperationAdd 
                                                                refId:[NSString stringWithFormat:@"%lld", group.groupId] 
                                                               params:json];
    [operationLog insert];
    [operationLog release];
    [[DataUploadService sharedInstance] run];
    
    return YES;
}


+ (BOOL) editGroup:(FriendGroup*)group
{
    //BOOL result = [TwitterClient updateUserGroupSync:group];

    [group updateDB];

    NSDictionary* dic = [group dictionary];
    NSString* json = [dic JSONRepresentation];
    
    OperationLog* operationLog = [[OperationLog alloc] initWithModule:kOperationLogModuleGroup 
                                                            operation:kOperationLogOperationEdit 
                                                                refId:[NSString stringWithFormat:@"%lld", group.groupId] 
                                                               params:json];
    [operationLog insert];
    [operationLog release];
    [[DataUploadService sharedInstance] run];
    
    return YES;
}


+ (BOOL) deleteGroup:(sqlite3_int64)groupId
{
    BOOL result = YES;
    //if ( sync ) {
    //    result = [TwitterClient destroyUserGroupSync:groupId];
    //}
    
    [FriendGroup deleteAllUserForGroup:groupId];
    [FriendGroup deleteGroup:groupId];
    
    NSString* json = [NSString stringWithFormat:@"{groupId:'%lld'}", groupId];
    
    OperationLog* operationLog = [[OperationLog alloc] initWithModule:kOperationLogModuleGroup 
                                                            operation:kOperationLogOperationDelete 
                                                                refId:[NSString stringWithFormat:@"%lld", groupId] 
                                                               params:json];
    [operationLog insert];
    [operationLog release];
    [[DataUploadService sharedInstance] run];

    return result;
}


+ (void) addUser:(User*)user toGroup:(sqlite3_int64)groupId
{
    if ( !user ) return;
    
    [FriendGroup insertGroupUser:user.userId groupId:groupId seq:0];
    
    NSString* json = [NSString stringWithFormat:@"{groupId:'%lld', userId:'%lld'}", groupId, user.userId];
    OperationLog* operationLog = [[OperationLog alloc] initWithModule:kOperationLogModuleGroup 
                                                            operation:kOperationLogOperationAddPersonToGroup 
                                                                refId:[NSString stringWithFormat:@"%lld", groupId] 
                                                               params:json];
    [operationLog insert];
    [operationLog release];
    [[DataUploadService sharedInstance] run];
    
    //NSArray* array = [NSArray arrayWithObject:[NSString stringWithFormat:@"%lld", groupId]];
    //[TwitterClient setUserGroupsSync:array userId:user.userId operation:@"add"];
}


+ (void) removeUser:(sqlite3_int64)userId fromGroup:(sqlite3_int64)groupId
{
    if ( userId <= 0 || groupId <= 0 ) return;
    
    [FriendGroup deleteGroupUser:userId groupId:groupId];

    NSString* json = [NSString stringWithFormat:@"{groupId:'%lld', userId:'%lld'}", groupId, userId];
    OperationLog* operationLog = [[OperationLog alloc] initWithModule:kOperationLogModuleGroup 
                                                            operation:kOperationLogOperationDeletePersonFromGroup 
                                                                refId:[NSString stringWithFormat:@"%lld", groupId] 
                                                               params:json];
    [operationLog insert];
    [operationLog release];
    [[DataUploadService sharedInstance] run];

    //NSArray* array = [NSArray arrayWithObject:[NSString stringWithFormat:@"%lld", groupId]];
    //[TwitterClient setUserGroupsSync:array userId:userId operation:@"remove"];
}


@end
